package counsel;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import code.CodeDAO;
import code.CodeDTO;
import salary.SalaryVo;
import Product.ProductVo;

public class SPSearch extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		//한글깨짐 처리
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");

		String sa_no = req.getParameter("sa_no");
		String pd_no = req.getParameter("pd_no");

		CounselDAO dao = new CounselDAO();
		
		int sum = 0;
		if(sa_no != null){//사원 검색 처리
			SalaryVo vo = new SalaryVo();
			vo.setSA_no(sa_no);
			SalaryVo sal = dao.salSearch(vo);
			if(sal == null){
				sal = new SalaryVo();
			}else{
				//대출 최대한도 구하는 로직
				Calendar cal = Calendar.getInstance();
				int year = cal.get(cal.YEAR);
				String temp = sal.getSA_sal().replace("만원", "");
				int money = Integer.parseInt(temp);
				sum += money / 300;
				if (sal.getSA_home().equals("Y")) sum += 10;
				if (sal.getSA_car().equals("Y")) sum += 3;
				if (sal.getSA_marr().equals("Y")) sum += 3;
				int HD = Integer.parseInt(sal.getSA_hd().substring(0, 4));
				sum += year-HD-1;
				
				sum = sum * 200;
				String max = "";
				if(sum>10000){
					sum=10000;
					max = "1억원";
				}else{
					max = sum+"만원";
				}
				sal.setSA_sal(sal.getSA_sal()+"만원");
				req.setAttribute("max", max);
			}
			req.setAttribute("sal", sal);
			
			
		}else if(pd_no != null){//상품 검색 처리
			ProductVo vo = new ProductVo();
			vo.setPD_no(pd_no);
			ProductVo pro = dao.proSearch(vo);
			if(pro == null) pro = new ProductVo();
			req.setAttribute("pro", pro);
		}
		
		//공통코드 관련
		CodeDTO code = new CodeDTO();
		CodeDAO cdao = new CodeDAO();
		//계약상태
		code.setCd_grp1("CTING");
		cdao.setDto(code);
		ArrayList<CodeDTO> codes = cdao.returnCode();
		req.setAttribute("ctings", codes);
		//직급
		cdao = new CodeDAO();
		code.setCd_grp1("PST");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("psts", codes);
		//부서
		cdao = new CodeDAO();
		code.setCd_grp1("SADP");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("sadps", codes);
				
		req.getRequestDispatcher("/intranet/counsel/counsel_sp_search.jsp").forward(req, resp);
	}

}
